package com.tiger.cmpeb.sample4;

import java.io.IOException;
import java.io.PrintWriter;

import javax.naming.InitialContext;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class Booking extends HttpServlet {

	private static final long serialVersionUID = 1L;

	public void init(ServletConfig config) throws ServletException {
		super.init(config);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		try {
			Integer reservationID = new Integer(request.getParameter("reservationID"));
			String reservationName = request.getParameter("reservationName");
			Integer flightID = new Integer(request.getParameter("flightID"));

			InitialContext ctx = new InitialContext();
			FlightHome01 flightHome = (FlightHome01) ctx.lookup("comp/env/ejb/FlightBean01");
			ReservationHome01 reservationHome = (ReservationHome01) ctx.lookup("comp/env/ejb/ReservationBean01");
			Reservation01 reservation = reservationHome.create(reservationID, reservationName);

			Flight01 flight = flightHome.findByPrimaryKey(flightID);
			reservation.setFlight(flight);
			
			response.sendRedirect("../com_tiger_cmpeb_sample4.html");

		} catch (Exception e) {
			PrintWriter out = response.getWriter();
			out.println("<html><body><pre>");
			e.printStackTrace(out);
			out.println("</pre></body></html>");
			out.close();
		}
	}
}
